package mi.payment.constants;

import android.text.TextUtils;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import mi.payment.utils.iso8583.Utils;


/**
 * @author Lee64 on 2017/3/7.
 */
public class TTLVUtils {


    /**
     * 将16进制字符串转换为TTLV对象map
     */
    public static Map<String, TTLV> builderTTLVMap(String hexString) {

        Map<String, TTLV> map = new HashMap<>();

        int position = 0;

        // 格式描述符
        String describe = hexString.substring(0, 2);

        // 0:单字节  1:双字节
        int tagType;
        int lenType;

        if (TextUtils.equals("00", describe)) {
            tagType = 0;
            lenType = 0;
        } else if (TextUtils.equals("01", describe)) {
            tagType = 0;
            lenType = 1;
        } else if (TextUtils.equals("10", describe)) {
            tagType = 1;
            lenType = 0;
        } else if (TextUtils.equals("11", describe)) {
            tagType = 1;
            lenType = 1;
        } else {
            throw new RuntimeException("TTLV 数据错误");
        }

        position += 2;

        while (position < hexString.length()) {

            // 取得子域tag标签
            String tag = getTag(hexString, position, tagType);
            position += tag.length();

            // 取得子域type标签
            String type = hexString.substring(position, position + 2);
            position += type.length();

            // 取得子域length标签
            String length = getLength(hexString, position, lenType);
            position += length.length();

            // 取得子域value标签
            String value;
            int index = Integer.parseInt(length, 16);
            int i = index % 2;
            if (TextUtils.equals("01", type)) {
                // BCD
                if (i == 0) {
                    value = hexString.substring(position, position + index);
                } else {
                    value = hexString.substring(position, position + index + 1);
                }
            } else if (TextUtils.equals("02", type)) {
                // ASCII
                value = hexString.substring(position, position + index * 2);
            } else if (TextUtils.equals("03", type)) {
                // BIN
                value = hexString.substring(position, position + index * 2);
            } else {
                throw new RuntimeException("TTLV type 解析错误");
            }

            position += value.length();

            TTLV ttlv = new TTLV();
            ttlv.setTag(tag);
            ttlv.setLength(length);
            ttlv.setType(type);
            ttlv.setValue(value);

            map.put(tag, ttlv);

        }

        return map;
    }

    /**
     * 取得子域Tag标签
     */
    private static String getTag(String hexString, int position, int type) {
        String firstByte;
        if (type == 0) {
            firstByte = hexString.substring(position, position + 2);
        } else {
            firstByte = hexString.substring(position, position + 4);
        }
        return firstByte;
    }

    /**
     * 取得子域length标签
     */
    private static String getLength(String hexString, int position, int type) {
        String length;
        if (type == 0) {
            length = hexString.substring(position, position + 2);
        } else {
            length = hexString.substring(position, position + 4);
        }
        return length;
    }

    /**
     * 16进制个gbk编码字符串解码
     */
    public static String decodeGBkHexString(String ascii) {
        byte[] bytes = Utils.hexStr2Bytes(ascii);
        try {
            return new String(bytes, "GBK");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return "";
    }

    public static String decodeGBkHexByte(byte[] bytes) {
        try {
            return new String(bytes, "GBK");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return "";
    }


}
